package com.authentication.code;

import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationToken;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class SimpleAuthenticationCodeRepository implements AuthenticationCodeRepository{

    private final Map<String,ClientAndUserAuthenticationPair> map = new ConcurrentHashMap<>();

    @Override
    public String storageCode(Authentication userAuthentication, OAuth2AuthorizationCodeRequestAuthenticationToken clientAuthenticationToken, String[] grantedScopes) {
        String code = UUID.randomUUID().toString();
        map.put(code, new ClientAndUserAuthenticationPair(userAuthentication,clientAuthenticationToken,grantedScopes));
        return code;
    }

    @Override
    public ClientAndUserAuthenticationPair get(String code) {
        return map.remove(code);
    }
}
